package com.dance.netty.netty.simple;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

public class NettyServer {

    public static void main(String[] args) throws InterruptedException {
        /*
         * 创建BossGroup 和 WorkerGroup
         * 说明:
         *  1. 创建连个线程组 bossGroup 和 workerGroup
         *  2. bossGroup只处理连接请求(accept), 真正的和客户端业务处理, 会交给workerGroup完成
         *  3. 两个都是无线循环
         *  4. bossGroup 和 workerGroup 含有的子线程(NioEventLoop)的个数
         * 默认: 实际CPU核数 * 2
         */
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();

        try {
            // 创建服务器端的启动对象, 配置参数
            ServerBootstrap serverBootstrap = new ServerBootstrap();

            serverBootstrap.group(bossGroup, workerGroup) //设置两个线程组
                    .channel(NioServerSocketChannel.class) // 使用NioServerSocketChannel作为服务器的通道实现
                    .option(ChannelOption.SO_BACKLOG, 128) // 设置线程队列等待连接个数
                    .childOption(ChannelOption.SO_KEEPALIVE, true) // 设置保持活动连接状态
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        /**
                         * 初始化通道
                         *  创建一个通道初始化对象(匿名对象)
                         *  给pipeline设置处理器
                         * @param socketChannel SocketChannel
                         * @throws Exception err
                         */
                        @Override
                        protected void initChannel(SocketChannel socketChannel) throws Exception {

                            System.out.println("socket channel hashcode is " + socketChannel.hashCode());
                            // 设置 处理器
                            socketChannel.pipeline().addLast(new NettyServerHandler());
                        }
                    }); // 给我们的workerGroup 的 EventLoop 对应的管道(pipeline)设置处理器

            System.out.println("server is ready......");

            // 绑定一个接口 并且同步 生成一个 ChannelFuture 对象
            // 启动服务器(并绑定端口)
            ChannelFuture channelFuture = serverBootstrap.bind(6668).sync();

            // 给channelFuture注册监听器
            channelFuture.addListener(new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture channelFuture) throws Exception {
                    // 如果操作成功
                    if (channelFuture.isSuccess()){
                        System.out.println("监听端口 6668 成功");
                    }else{
                        System.out.println("监听端口 6668 失败");
                    }
                }
            });

            // 对关闭通道进行监听
            channelFuture.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }

}
